﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace Defero.Web
{
    // Nota: para obtener instrucciones sobre cómo habilitar el modo clásico de IIS6 o IIS7, 
    // visite http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Nombre de ruta
                "{controller}/{action}/{id}", // URL con parámetros
                new { controller = "BugTracker", action = "MyBugs", id = UrlParameter.Optional } // Valores predeterminados de parámetro
            );

            //routes.MapRoute(
            //    "Section", 
            //    "{controller}/{id}",
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional })
            //;

        }

        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            ControllerBuilder.Current.SetControllerFactory(typeof(DeferoControllerFactory));
        }

    }
    /// <summary>
    /// Fabrica de controles.
    /// </summary>
    public class DeferoControllerFactory : DefaultControllerFactory
    {
        /// <summary>
        /// Contenedor de instancias.
        /// </summary>
        Microsoft.Practices.Unity.IUnityContainer container;
        /// <summary>
        /// Constructor default.
        /// </summary>
        public DeferoControllerFactory() 
        {
            container = new Microsoft.Practices.Unity.UnityContainer();
            BuildContainer();
        }
        /// <summary>
        /// Devuelve una instancia de controlador.
        /// </summary>
        /// <param name="requestContext">Contexto de la petición.</param>
        /// <param name="controllerType">Tipo del controlador.</param>
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                throw new HttpException(404, "Controller not found");

            return container.Resolve(controllerType) as IController;
        }
        /// <summary>
        /// Genera el contenedor.
        /// </summary>
        protected void BuildContainer() 
        {

            container.RegisterInstance<System.Data.Common.DbConnection>(
                new System.Data.EntityClient.EntityConnection("name=QueryDatabase")
            );

            UnityConfigurationSection unityConfiguration = (UnityConfigurationSection)
                ConfigurationManager.GetSection("enterpriseLibrary.unityConfiguration");

            unityConfiguration.Configure(container);

            container.RegisterType<Defero.Web.Controllers.BugTrackerController>();
            container.RegisterType<Defero.Web.Controllers.HomeController>();

        }

    }
}